package it.koutifaris.travianmap.context;

import android.app.Activity;

public class ContextRetreiver {

	private static final ContextRetreiver INSTANCE = new ContextRetreiver();
	private TravianMapContextImpl context;

	private ContextRetreiver() {
	}

	public static ContextRetreiver contextRetreiver() {
		return INSTANCE;
	}

	public void setCurrentContext(Activity activity) {
		String serverId = getCurrentServerId();
		closeCurrentDb();
		this.context = new TravianMapContextImpl(activity);
		this.context.setCurrentServerId(serverId);
	}

	private String getCurrentServerId() {
		String serverId = null;

		if (context != null) {
			serverId = context.getCurrentServerId();
		}

		return serverId;
	}

	private void closeCurrentDb() {
		if (context != null) {
			context.getCurrentSqlMapDbHelper().close();
		}
	}

	public void setCurrentServerId(String serverId) {
		context.setCurrentServerId(serverId);
	}

	public TravianMapContext getCurrentContext() {
		return this.context;
	}

}
